package com.qingshanhui.home_module.module3;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qingshanhui.home_module.module1.TopBannerModel;
import com.qingshanhui.home_module.module2.RecommendModel;
import com.qingshanhui.home_module.module2.RecommendViewHolder;
import com.qingshanhui.home_module.utils.Banner;
import com.qingshanhui.home_module.utils.DisplayUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Desc:推荐
 * <p>
 * Author: AnYaBo
 * Date: 2020/1/19
 * Copyright: Copyright (c) 2016-2022
 * Company: @小牛科技
 * Email:anyabo@xiaoniu.com
 * Update Comments:
 */
public class ActivityWidget extends LinearLayout {
    private Banner mBanner;

    public ActivityWidget(Context context) {
        this(context,null);
    }

    public ActivityWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    private void initViews(Context context) {
        setOrientation(VERTICAL);
        LayoutParams titleParams = new LayoutParams(-2,-2);
        int margin = (int) DisplayUtils.dip2px(context,12);
        titleParams.leftMargin = margin;
        titleParams.topMargin = margin;
        titleParams.bottomMargin = margin;
        mBanner = new Banner(context);
        setPadding(0,margin,0,0);
        LayoutParams bannerParams = new LayoutParams(-2,-2);
        addView(mBanner,bannerParams);
    }

    public void setData(int rowCount,int pageMaxCount,List<ActivityModel> recommendModels){
        if (recommendModels == null || recommendModels.size() == 0){
            return;
        }

        List<List<ActivityModel>> activityPageModelList = new ArrayList<>();
        if (pageMaxCount > 0){
            int length = recommendModels.size();
            int num = ( length + pageMaxCount - 1 )/pageMaxCount ;
            for (int i = 0; i < num; i++) {
                int fromIndex = i * pageMaxCount;
                int toIndex = Math.min((i + 1) * pageMaxCount, length);
                activityPageModelList.add(recommendModels.subList(fromIndex,toIndex)) ;
            }
        }
        mBanner.setData(activityPageModelList, new ActivityViewHolder(rowCount))
                .setIndicatorRes(android.R.color.holo_green_light,android.R.color.darker_gray)
                .setIndicatorWidthAndHeight(30,5)
                .setLoopDuration(6000)
                .setOnBannerClickListener(new Banner.OnBannerClickListener() {
                    @Override
                    public void onBannerClick(List<Object> data, int position) {
                        TopBannerModel topBannerModel = (TopBannerModel) data.get(position);
                        Toast.makeText(getContext(),topBannerModel.url,Toast.LENGTH_SHORT).show();
                    }
                })
                .start();
    }

}
